//订单提交
let express = require('express');
let path = require('path');
//引入自定义模块
let user = require('../model/user');
let order = require('../model/order');
let seller = require('../model/seller');

var router = express.Router();

var format = function (a){
    if(a<10)
        return '0'+ a;
    else
        return a;
}

router.get('/',function(req,res,next){
    res.sendStatus(404);
    //_dirname:当前文件的路径，path.join():合并路径
});
/**
 *登录验证功能
 */
router.post('/',function (req,res,next){
    let userID = req.body.userID;
    let token = req.body.token;
    if(user.checkToken(userID,token) === false)
        res.send({submitStatus:2});
    else {
        let sellerID = req.body.sellerID;
        let orderState = req.body.orderState;
        let cart = req.body.cart;
        let senderInfo = '{"dManID":"2","expectedDeliveryTime":"3:00","practiceDeliveryTime":"3:00"}';
        let contactInfo = JSON.stringify(req.body.contactInfo);
        order.searchMaxOrderID(function (oi){
            if(oi == undefined)
                var orderID = 1;
            else
                var orderID = parseInt(oi) + 1;
            seller.search(sellerID,function (result){
                if(result.length === 0){
                    res.send({submitStatus:1});
                }
                else {
                    let sellerName = 'wht';
                    let billSum = 0;
                    let cartlen = cart.length;
                    var str = '';
                    for(i=0;i<cartlen-1;i++){
                        str += JSON.stringify(cart[i]);
                        billSum += cart[i].thingNumber * cart[i].singlePrice;
                        str += '|';
                    }
                    billSum += cart[cartlen-1].thingNumber * cart[cartlen-1].singlePrice;
                    cart.forEach((item)=>{
                        billSum+=item.thingNumber*2
                    })
                    billSum = billSum.toFixed(2);
                    str += JSON.stringify(cart[cartlen-1]);
                    cart = str;
                    let time = new Date();
                    let createTime = time.getFullYear()+'-'+
                        format(time.getMonth()+1) + '-' +
                        format(time.getDate()) + ' ' +
                        format(time.getHours())+':'+format(time.getMinutes())
                        +':'+format(time.getSeconds());
                    let Order = {
                        userID: userID,
                        orderID: orderID,
                        sellerID: sellerID,
                        sellerName: sellerName,
                        billSum: billSum,
                        createTime: createTime,
                        orderState: orderState,
                        cart: cart,
                        senderInfo: senderInfo,
                        contactInfo: contactInfo
                    }
                    order.add(Order);
                    res.send({submitStatus: 0,orderID:orderID});
                }
            })
        })
    }


})
module.exports = router;